//Copyright (C) 2010 Workplicity, contact: ronncoleman@gmail.com
//
//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either
//version 3 of the License, or (at your option) any later version.
//
//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this library; if not, write to the Free Software
//Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

package org.workplicity.marist.print;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.print.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import org.workplicity.entry.WorkSlate;
import org.workplicity.marist.grounds.GroundsRequest;
import org.workplicity.util.WorkDate;
/**
 *
 * @author Ryan Flaherty
 */
public class MainPrinter implements Printable {
    protected static final int PAGE_WIDTH = 612;
    protected static final int PAGE_HEIGHT = 792;
    protected static final int IMAGE_WIDTH = 2550;
    protected static final int IMAGE_HEIGHT = 3300;
    protected static final int WIDTH_INFINITE = 0;
    protected static final int FIELD_NO_X = 510;
    protected static final int FIELD_NO_Y = 44;
    protected static final int FIELD_REQUESTEDBY_X = 29;
    protected static final int FIELD_REQUESTEDBY_Y = 71;
    protected static final int WIDTH_REQUESTEDBY = 10;
    protected static final int FIELD_APPROVEDBY_X = 90;
    protected static final int FIELD_APPROVEDBY_Y =71;
    protected static final int WIDTH_APPROVEDBY = 11;
    protected static final int FIELD_LOCATION_X = 307;
    protected static final int FIELD_LOCATION_Y = 71;
    protected static final int WIDTH_LOCATION = 12;
    protected static final int FIELD_CAUSEOFDAMAGE_X = 380;
    protected static final int FIELD_CAUSEOFDAMAGE_Y = 71;
    protected static final int WIDTH_CAUSEOFDAMAGE = 35;
    protected static final int FIELD_ESTHOURS_X = 422;
    protected static final int FIELD_ESTHOURS_Y = 206;
    protected static final int FIELD_ACTHOURS_X = 423;
    protected static final int FIELD_ACTHOURS_Y = 218;
    protected static final int FIELD_PRIORITY_X = 70;
    protected static final int FIELD_PRIORITY_Y = 230;
    protected static final int WIDTH_PRIORITY = 51;
    protected static final int FIELD_REASON_X = 70;
    protected static final int FIELD_REASON_Y = 242;
    protected static final int WIDTH_REASON = 51;
    protected static final int FIELD_WSNAME_X = 84;
    protected static final int FIELD_WSNAME_Y = 94;
    protected static final int WIDTH_WSNAME = 16;
    protected static final int FIELD_WSSTATUS_X = 218;
    protected static final int FIELD_WSSTATUS_Y =94;
    protected static final int WIDTH_WSSTATUS = 21;
    protected static final int FIELD_WSSIZE_X = 424;
    protected static final int FIELD_WSSIZE_Y = 94;
    protected static final int WIDTH_WSSIZE = 16;
    protected static final int FIELD_NUMUSERS_X = 550;
    protected static final int FIELD_NUMUSERS_Y = 94;
    protected static final int WIDTH_NUMUSERS = 5;
    protected static final int FIELD_CHARGETO_X = 88;
    protected static final int FIELD_CHARGETO_Y = 208;
    protected static final int WIDTH_CHARGETO = 48;
    protected static final int FIELD_ISSUEDTO_X = 435;
    protected static final int FIELD_ISSUEDTO_Y = 193;
    protected static final int FIELD_DATEASSIGNED_X = 455;
    protected static final int FIELD_DATEASSIGNED_Y = 169;
    protected static final int FIELD_DATECOMPLETED_X = 440;
    protected static final int FIELD_DATECOMPLETED_Y = 180;
    protected static final int FIELD_DATEUPDATED_X = 158;
    protected static final int FIELD_DATEUPDATED_Y = 71;
    protected static final int FIELD_WORKREQUIRED_X = 29;
    protected static final int FIELD_WORKREQUIRED_Y = 117;
    protected static final int HEIGHT_WORKREQUIRED = 10;
    protected static final int WIDTH_WORKREQUIRED = 59;
    protected static final int FIELD_MATERIALS_X = 379;
    protected static final int FIELD_MATERIALS_Y = 117;
    protected static final int HEIGHT_MATERIALS = 6;
    protected static final int WIDTH_MATERIALS = 35;


    protected static final int YOFFSET = 262;
    protected static final int YOFFSET2 = 526;
    

    protected String workNumberS;
    protected String requestedBy;
    protected String approvedBy;
    protected String date;
    protected String location;
    protected String causeOfDamage;
    protected String workRequired;
    protected String materials;
    protected WorkDate dateUpdated;
    protected WorkDate dateAssigned;
    protected WorkDate dateCompleted;
    protected String issuedTo;
    protected String chargeTo;
    protected String estHours;
    protected String actHours;
    protected String priority;
    protected String reason;
    protected String wsName;
    protected String wsStatus;
    protected String wsSize;
    protected String numUsers;
    protected Font cNPlain = new Font ("Courier New", Font.PLAIN, 10);
    private final ArrayList<GroundsRequest> requests;
    private final ArrayList<WorkSlate> workslates;
    private final ArrayList<Integer> numsUsers;
    private final JFrame parent;

    //Constructor
    public MainPrinter(JFrame parent, ArrayList<GroundsRequest> requests, ArrayList<WorkSlate> workslates, ArrayList<Integer> numsUsers){
        this.parent = parent;
        this.requests = requests;
        this.workslates = workslates;
        this.numsUsers = numsUsers;
    }
    
    public int print (Graphics g, PageFormat pf, int page) throws PrinterException {
        int yOffset = 0;
        //try using the page variable instead of setNumber

        // Don't print more than the number of pages
        // 'page' parameter is zero-based
        System.out.println("page = "+page+ " requests.size()"+requests.size());
        if (page*3>=requests.size()){
            return NO_SUCH_PAGE;
        }

        
        /* User (0,0) is typically outside the imageable area, so we must
        * translate by the X and Y values in the PageFormat to avoid clipping
        */
        Graphics2D g2d = (Graphics2D)g;
        g2d.setFont(cNPlain);
        
        //set up the PrintHelper
        PrintHelper printHelper = new PrintHelper(g2d);
       
        /* Now we perform our rendering */
        try {

            //FileInputStream is = new FileInputStream("MaristGrounds.png");
            InputStream is = this.getClass().getResourceAsStream( "MaristGrounds.png" );
            BufferedImage image = ImageIO.read(is);
            //Fit image onto the page
            //try caching this usign g.create to make a copy
            g.drawImage(image, 0, 0, PAGE_WIDTH, PAGE_HEIGHT, 0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, null);

            for (int cardNo = 0;cardNo<3;cardNo++){
                System.out.println("vieweing request#"+(page*3+cardNo));
                if (page*3+cardNo<requests.size()){
                    // Initialize the page parameters with this request
                initPageParameters(requests.get(page*3+cardNo),workslates.get(page*3+cardNo),numsUsers.get(page*3+cardNo));
                //Set an offset baised on what card is to be printed
                if (cardNo==0){
                    yOffset = 0;
                }else if(cardNo==1){
                    yOffset = YOFFSET;
                }else if(cardNo==2){
                    yOffset = YOFFSET2;
                }


                //Must add text after drawing image, or text will be behind image.
                printHelper.PrintLine(workNumberS, FIELD_NO_X, FIELD_NO_Y+yOffset, WIDTH_INFINITE);
                printHelper.PrintLine(requestedBy, FIELD_REQUESTEDBY_X, FIELD_REQUESTEDBY_Y+yOffset, WIDTH_REQUESTEDBY);
                printHelper.PrintLine(approvedBy, FIELD_APPROVEDBY_X, FIELD_APPROVEDBY_Y+yOffset, WIDTH_APPROVEDBY);
                printHelper.PrintLine(location, FIELD_LOCATION_X, FIELD_LOCATION_Y+yOffset, WIDTH_LOCATION);
                printHelper.PrintLine(causeOfDamage, FIELD_CAUSEOFDAMAGE_X, +FIELD_CAUSEOFDAMAGE_Y+yOffset, WIDTH_CAUSEOFDAMAGE);
                printHelper.PrintLine(estHours, FIELD_ESTHOURS_X, FIELD_ESTHOURS_Y+yOffset, WIDTH_INFINITE);
                printHelper.PrintLine(actHours, FIELD_ACTHOURS_X, FIELD_ACTHOURS_Y+yOffset, WIDTH_INFINITE);
                printHelper.PrintLine(priority, FIELD_PRIORITY_X, FIELD_PRIORITY_Y+yOffset, WIDTH_PRIORITY);
                printHelper.PrintLine(reason, FIELD_REASON_X, FIELD_REASON_Y+yOffset, WIDTH_REASON);
                printHelper.PrintLine(wsName, FIELD_WSNAME_X, FIELD_WSNAME_Y+yOffset, WIDTH_WSNAME);
                printHelper.PrintLine(wsStatus, FIELD_WSSTATUS_X, FIELD_WSSTATUS_Y+yOffset, WIDTH_WSSTATUS);
                printHelper.PrintLine(wsSize, FIELD_WSSIZE_X, FIELD_WSSIZE_Y+yOffset, WIDTH_WSSIZE);
                printHelper.PrintLine(numUsers, FIELD_NUMUSERS_X, FIELD_NUMUSERS_Y+yOffset, WIDTH_NUMUSERS);
                printHelper.PrintLine(chargeTo, FIELD_CHARGETO_X, FIELD_CHARGETO_Y+yOffset,WIDTH_CHARGETO);
                printHelper.PrintLine(issuedTo, FIELD_ISSUEDTO_X, FIELD_ISSUEDTO_Y+yOffset, WIDTH_INFINITE);
                printHelper.PrintDate(dateAssigned, FIELD_DATEASSIGNED_X, FIELD_DATEASSIGNED_Y+yOffset, false);
                printHelper.PrintDate(dateCompleted, FIELD_DATECOMPLETED_X, FIELD_DATECOMPLETED_Y+yOffset, false);
                printHelper.PrintDate(dateUpdated, FIELD_DATEUPDATED_X, FIELD_DATEUPDATED_Y+yOffset, true);
                printHelper.PrintBox(workRequired, FIELD_WORKREQUIRED_X, FIELD_WORKREQUIRED_Y+yOffset, HEIGHT_WORKREQUIRED, WIDTH_WORKREQUIRED);
                printHelper.PrintBox(materials, FIELD_MATERIALS_X, FIELD_MATERIALS_Y+yOffset, HEIGHT_MATERIALS, WIDTH_MATERIALS);
                }
            }
        }

        catch (IOException ex) {
            Logger.getLogger(MainPrinter.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        /* tell the caller that this page is part of the printed document */
        return PAGE_EXISTS;

    }

    private void initPageParameters(GroundsRequest request,WorkSlate workslate, int nusers) {
        //What happenes when this gets too long?  Should we truncate it?
        workNumberS = request.getId().toString();
        //Requesy by: field has a max of 8 characters
        requestedBy = request.getRequestedBy();
        //Approved by: field has a max of 9 characters
        approvedBy = request.getApprovedBy();
        //Location field has a max of 10 characters
        location = request.getLocation();

        causeOfDamage = request.getDamageCause();
        //Can have up to 10 lines of 49 characters
        workRequired = request.getDescr();
        //Can have up tp 6 lines of 29 characters
        materials = request.getMaterials();
        dateUpdated = request.getUpdateDate();

        reason = request.getReason();
        priority = request.getPriority().toString();
        estHours = Double.toString(request.getEstHours());
        actHours = Double.toString(request.getActHours());
        dateAssigned = request.getAssignedDate();
        dateCompleted = request.getCompleteDate();
        this.numUsers = Integer.toString(nusers);
        chargeTo = request.getChargeTo();
        if (workslate!=null){
//            ws = workslate;
//            //wsName = ws.getName();
            wsName = workslate.getName();
            issuedTo = workslate.getName();
            wsStatus = request.getStatus().toString();
            wsSize = Integer.toString(workslate.getWorkRequestList().size());
        }
        else{
            wsName = "NONE";
            wsStatus = request.getStatus() + "";
            wsSize = "N/A";
            issuedTo = "N/A";
        }
    }
}
